package com.wn.inventory.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.common.*;
import com.wn.entity.Hr;
import com.wn.entity.OutWarehouseApplication;
import com.wn.inventory.feign.ActivitiFeignClient;
import com.wn.inventory.mapper.OutWarehouseApplicationMapper;
import com.wn.inventory.service.OutWarehouseApplicationService;
import com.wn.inventory.vo.OutWarehouseApplicationVo;
import com.wn.utils.CurrentTime;
import com.wn.utils.PageBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ author     ：NST
 * @ date       ：Created in 2022/10/21 16:36
 * @ description：
 * @ modified By：
 * @ version:
 */
@Service
public class OutWarehouseApplicationImpl implements OutWarehouseApplicationService {
    @Autowired
    OutWarehouseApplicationMapper outWarehouseApplicationMapper;
    @Autowired
    ActivitiFeignClient activitiFeignClient;

    @Override
    public void insert(OutWarehouseApplication outWarehouseApplication, HttpServletRequest request) {
        outWarehouseApplication.setApplicant(new Hr(Integer.parseInt(request.getHeader("userId"))));
        outWarehouseApplication.setOutTime(CurrentTime.getCurrentTime());
        String identifier="OWA"+CurrentTime.getCurrentTimeString();
        outWarehouseApplication.setIdentifier(identifier);
        ResponseEntity<String> entity = activitiFeignClient.startInstance(new ActivitiStartEntity("in_warehouss_application_id", "user1", request.getHeader("userWorkNumber")));
//        activitiFeignClient.completeTask(new ActivitiCompleteEntity("in_warehouss_application_id",entity.getData(),"user1",));
        outWarehouseApplication.setActivitiInstanceId(entity.getData());
        outWarehouseApplicationMapper.insert(outWarehouseApplication);
    }

    @Override
    public PageBean<OutWarehouseApplication> getByCondition(OutWarehouseApplicationVo outWarehouseApplicationVo) {
        Page<OutWarehouseApplication> page = PageHelper.startPage(outWarehouseApplicationVo.getCurrPage(), outWarehouseApplicationVo.getPageSize());
        List<OutWarehouseApplication> list = outWarehouseApplicationMapper.getByCondition(outWarehouseApplicationVo.getOutWarehouseApplication());
        //创建新集合 用于存放远程调用需要的参数 流程id集合
        List<ActivitiHistoryInfoEntity> entityList = new ArrayList<>();
        //把查询数据库得到的集合中的元素的流程id加入entityList
        list.stream().forEach(l->entityList.add(new ActivitiHistoryInfoEntity(l.getActivitiInstanceId())));
        //远程调用获取流程历史信息的集合
        ResponseEntity<List<List<ActivitiInstanceHistory>>> historyInfoList = activitiFeignClient.historyInfoList(entityList);
        //获取每个流程的流程状态信息 存为string集合
        List<String> activitiNames = historyInfoList.getData()
                .stream()
                .map(h -> h.get(h.size() - 1).getActivitiName())
                .collect(Collectors.toList());
        //在数据库查询到的集合中再挨个存入流程状态
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setActivitiState(activitiNames.get(i));
        }
        return PageBeanUtil.getPageBean(list, page, outWarehouseApplicationVo.getCurrPage(), outWarehouseApplicationVo.getPageSize());
    }
}
